﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace THenu.WindowsEffectLib
{
    /// <summary>
    /// 窗口标题和任务栏闪烁
    /// </summary>
    public partial class FrmFlashEffect : Form
    {
        /// <summary>
        /// 闪烁窗口
        /// </summary>
        /// <param name="pwfi"></param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        static extern bool FlashWindowEx(ref FLASHWINFO pwfi);
        /// <summary>
        /// 闪烁一次
        /// </summary>
        /// <param name="handle"></param>
        /// <param name="invert"></param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        static extern bool FlashWindow(IntPtr handle, bool invert);
        /// <summary>
        /// 停止闪动.系统将窗体恢复到初始状态.
        /// </summary>
        public const UInt32 FLASHW_STOP = 0;
        /// <summary>
        /// 闪动窗体的标题.
        /// </summary>
        public const UInt32 FLASHW_CAPTION = 1;
        /// <summary>
        /// 连续不停的闪动,直到此参数被设置为:FLASHW_STOP
        /// </summary>
        public const UInt32 FLASHW_TRAY = 2;
        /// <summary>
        /// 闪动窗体标题和任务栏按钮,等于将此参数设置为:FLASHW_CAPTION | FLASHW_TRAY.
        /// </summary>
        public const UInt32 FLASHW_ALL = 3;
        /// <summary>
        /// 连续不停的闪动,直到此参数被设置为:FLASHW_STOP
        /// </summary>
        public const UInt32 FLASHW_TIMER = 4;
        /// <summary>
        /// 连续不停的闪动,直到窗体用户被激活.通常用法将参数设置为: FLASHW_ALL | FLASHW_TIMERNOFG
        /// </summary>
        public const UInt32 FLASHW_TIMERNOFG = 12;
        private void button1_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
            // 创建传递参数
            FLASHWINFO fInfo = new FLASHWINFO();
            fInfo.cbSize = Convert.ToUInt32(Marshal.SizeOf(fInfo));
            fInfo.hwnd = this.Handle;
            // 这里是闪动窗标题和任务栏按钮,直到用户激活窗体
            fInfo.dwFlags = FLASHW_TRAY | FLASHW_TIMERNOFG;
            fInfo.uCount = UInt32.MaxValue;
            fInfo.dwTimeout = 0;

            FlashWindowEx(ref fInfo);
        }
        public FrmFlashEffect()
        {
            InitializeComponent();
        }
    }
    /// <summary>
    /// 传递的结构类型
    /// </summary>
    public struct FLASHWINFO
    {
        /// <summary>
        /// cbSize结构体FLASHWINFO的大小.
        /// </summary>
        public UInt32 cbSize;
        /// <summary>
        /// hwnd要闪动的窗体的句柄.窗体可以是打开的也可以是最小化的.
        /// </summary>
        public IntPtr hwnd;
        /// <summary>
        /// 闪动的状态/标志. 此参数可以是以下的值或值的组合(用或操作符"|"组合).
        /// </summary>
        public UInt32 dwFlags;
        /// <summary>
        /// 闪动的次数.
        /// </summary>
        public UInt32 uCount;
        /// <summary>
        /// 闪动频率,单位毫秒. 如果dwTimeout值为0, 函数使用光标的闪动频率.
        /// </summary>
        public UInt32 dwTimeout;
    }
}
